#include "Modifies.h"
#include <set>

Modifies::Modifies(){
	this->modifiesStmtIndex = vector<pair<int, int>>();
	this->modifiesProcIndex = vector<pair<int, int>>();
}

int Modifies::addStmtModifies(int stmtNum, int varIndex){
	if(stmtNum <= 0 || varIndex < 0){
		return -1;
	}
	this->modifiesStmtIndex.push_back(make_pair(stmtNum, varIndex));
	return modifiesStmtIndex.size()-1;
}

int Modifies::addProcModifies(int procIndex, int varIndex){
	if(procIndex < 0 || varIndex < 0){
		return -1;
	}
	this->modifiesProcIndex.push_back(make_pair(procIndex, varIndex));
	return modifiesProcIndex.size()-1;
}

vector<pair<int, int>> Modifies::getModifiesStatement(int stmtNumber, int varIndex){
	/* current: cater for assignment and container statements only.
	 *			(for procedures, use getUsesProcedure() instead)
	 * future: cater for call statements. 
	 */
	set<pair<int, int>>* matchingPairs = new set<pair<int, int>>();
	for(vector<pair<int, int>>::iterator it = modifiesStmtIndex.begin(); it != modifiesStmtIndex.end(); it++) {
		if((it->first == stmtNumber || stmtNumber == ANYVALUE) && 
			(it->second == varIndex || varIndex == ANYVALUE)){
				matchingPairs->insert(*it);
		}
	}
	return vector<pair<int, int>>(matchingPairs->begin(), matchingPairs->end());
}

vector<pair<int, int>> Modifies::getModifiesProcedure(int procIndex, int varIndex){
	/* current: cater for directly procedure only.
	 * future: cater for transitive procedure evaluation.
	 */
	set<pair<int, int>>* matchingPairs = new set<pair<int, int>>();
	for(vector<pair<int, int>>::iterator it = modifiesProcIndex.begin(); it != modifiesProcIndex.end(); it++) {
		if((it->first == procIndex || procIndex == ANYVALUE) && 
			(it->second == varIndex || varIndex == ANYVALUE)){
				matchingPairs->insert(*it);
		}
	}
	return vector<pair<int, int>>(matchingPairs->begin(), matchingPairs->end());
}